import os

from pynput.mouse import Button


def file_cache(filename):
    def fun1(fn):
        def fun2(*args, **kwargs):
            curPath = os.path.abspath(os.path.dirname(__file__))
            rootPath = curPath[:curPath.find("NewTest\\") + len("NewTest\\")]  # 获取项目的根路径
            cache_path = os.path.abspath(rootPath + f'tmp\\{filename}.txt')
            res = fn(*args, **kwargs)
            with open(cache_path, "a", encoding="utf-8") as f:
                # 鼠标按键对应信息转换 例如Button.left转换为mouse_left
                data = []
                for arg in args:
                    if Button.left == arg:
                        arg = 'mouse_left'
                    elif Button.right == arg:
                        arg = 'mouse_right'
                    elif Button.middle == arg:
                        arg = 'mouse_middle'
                    data.append(arg)
                # 强制类型转换
                result = str(data[1:])
                f.write(result+'\n')
            return res
        return fun2
    return fun1
